package wili.insertCode;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.function.Consumer;

import org.apache.commons.io.FileUtils;

import wili.c.Ca;
import wili.c.File9a;

/**
 * @author weila 2022年6月23日
 */
class InsertTimeCostMethod_rollback_main {
	public InsertTimeCostMethod_rollback_main() {

	}

	static void rollbackPatchedFile() {
		/**
		 *
		 */
		final int[] fileNum = { 0, 0 };
		Coms2.log("================start rollbackFile...");
		File9a.traverseFile(Coms2.srcBackupRoot, new Consumer<File>() {
			@Override
			public void accept(File bfile) {
				File jfile = Coms2.getJavaFileOfBackupFile(bfile);
				if (!jfile.exists())
					return;
				if (!jfile.getName().endsWith(".java"))
					return;
				try {
					Coms2.log(-1, " check file:", bfile.getName());
					if (bfile.getName().equals("ExcelGenerator.java"))
						Coms2.pause();
					++fileNum[0];
					String s0 = FileUtils.readFileToString(jfile, Charset.defaultCharset());
					if (s0.startsWith(Coms2.PreMark)) {
						++fileNum[1];
						Coms2.log(" " + fileNum[1] + ", rollbackFile:" + bfile.getName() + "~>" + jfile);
						FileUtils.copyFile(bfile, jfile);
					} else if ("123".isEmpty() && jfile.lastModified() != bfile.lastModified()) {
						Coms2.log(" " + bfile.getName() + " in " + jfile.getParentFile().getAbsolutePath());
						String ret = Ca.readCmdLine("  is updated, replace by backup file?");
						if (ret.equalsIgnoreCase("Y")) {
							FileUtils.copyFile(bfile, jfile);
							++fileNum[1];
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		Coms2.log(" allFileNum=" + fileNum[0] + ", " + fileNum[1] + " file rollbacked.");
	}

	public void start() {
		int opt = 1;
		InsertTimeCostMethod_rollback_main app = new InsertTimeCostMethod_rollback_main();
		Coms2.srcBackupRoot = new File("c:/t/InsertTimeCostMethod_main");
		Coms2.file_copiedFilesList = new File(Coms2.srcBackupRoot, "CopiedFilesList");
		Coms2.getClassFromJava = null;
		rollbackPatchedFile();
		Coms2.log("end.");
	}

	public static void main(String[] args) {
		//rollback patched file.
		InsertTimeCostMethod_rollback_main main = new InsertTimeCostMethod_rollback_main();
		main.start();
	}
}
